﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Bill;
using OpenSoftStore.Client.Common;

namespace OpenSoftStore.Client.UIL.ManageReport
{
    public partial class ReportProduct : UserControl
    {
        private BillLogic billLogic;
        private static ReportProduct instance;
        public static ReportProduct Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new ReportProduct();
                }
                return instance;
            }
        }

        public ReportProduct()
        {
            billLogic = new BillLogic();
            InitializeComponent();

            dgvProduct.AutoGenerateColumns = false;
            dgvProductGroup.AutoGenerateColumns = false;
            //dgvProduct.DataSource = billLogic.GetProductReport(" where  T_Bill.AddTime>=#" + DateTime.Now.Date + "#");
            dgvProductGroup.DataSource = billLogic.GetProductGroupReport(" where  T_Bill.AddTime>=#" + DateTime.Now.Date + "#");
            SPACommon.SetDataGridStyle(dgvProductGroup);
            SPACommon.ShowSummary(dgvProductGroup);

            dgvProduct.DataSource = billLogic.GetProductReport(" where 1=2 ");
            SPACommon.SetDataGridStyle(dgvProduct);
            SPACommon.ShowSummary(dgvProduct);
        }

        private void glassButton1_Click(object sender, EventArgs e)
        {
            DateTime from = dtpFrom.Value.Date;
            DateTime to = dtpTo.Value.Date;

            string whereStr = string.Format(" where T_Bill.AddTime between #{0}# and #{1}#  ", from, to);
            dgvProductGroup.AutoGenerateColumns = false;
            dgvProductGroup.DataSource = billLogic.GetProductGroupReport(whereStr);
        }

        private void dgvProductGroup_SelectionChanged(object sender, EventArgs e)
        {
            DateTime from = dtpFrom.Value.Date;
            DateTime to = dtpTo.Value.Date;
            if (dgvProductGroup.CurrentRow==null) return;
            var productId = dgvProductGroup.CurrentRow.Cells["ProductId"].Value.ToString();
            string whereStr = string.Format(" where T_Bill.AddTime between #{0}# and #{1}#  and T_BillItem.ProductId ='{2}'", from, to,productId);
            dgvProduct.AutoGenerateColumns = false;
            dgvProduct.DataSource = billLogic.GetProductReport(whereStr);

        }

        private void btnOutput_Click(object sender, EventArgs e)
        {
            SPACommon.ExportToExcel(dgvProductGroup, "商品服务使用情况");
        }
    }
}
